import re
import time
import os
from watchdog.events import FileSystemEventHandler
from watchdog.observers import Observer
from instruction.utils import load_json_file
from configs.other_config import REGEX_FILE_PATH, REGEX_FOLDER_PATH


# 预编译正则规则
def precompiled_regex_relus():
    # 存储编译后的规则
    rules = {}

    # 遍历意图和规则
    for intent, intent_rules in regex_rules.items():
        compiled_intent_rules = [re.compile(rule, re.IGNORECASE) for rule in intent_rules]
        rules[intent] = compiled_intent_rules

    return rules


# 读取正则json文件
regex_rules = load_json_file(REGEX_FILE_PATH)
# 预编译正则规则
try:
    compiled_rules = precompiled_regex_relus()
    # 正则json文件是否正常
    regex_json = True
except:
    regex_json = False


class FileEventHandler(FileSystemEventHandler):
    def on_modified(self, event):
        global regex_rules, compiled_rules, regex_json
        if event.src_path == REGEX_FILE_PATH:
            try:
                regex_json = True
                # 读取正则json文件
                regex_rules = load_json_file(REGEX_FILE_PATH)
                # 预编译正则规则
                compiled_rules = precompiled_regex_relus()
                # 文件修改时间
                modification_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
                log_message = f"{os.path.basename(REGEX_FILE_PATH)} modified at {modification_time}"
                print(log_message)
            except:
                regex_json = False
                print(f"There is a problem with the {os.path.basename(REGEX_FILE_PATH)} file.")


observer = Observer()
event_handler = FileEventHandler()
observer.schedule(event_handler, REGEX_FOLDER_PATH, True)
observer.start()

# 创建聊天室的正则规则
create_chatroom_regex_rules = [
    re.compile(r'.*创建(聊天|语聊)(室|房).*')
]

# 加入指定聊天室的正则规则
join_chatroom_regex_relus = [
    re.compile(r'.*(加入|打开|进入|前往)(.*?)的?((聊天|语聊)(室|房)?|房间).*')
]


# 创建语聊室a
async def create_chatroom_relu(prompt):
    create_chatroom_relu.matching_type = 'regex'
    try:
        for i in compiled_rules["create_chatroom"]:
            if re.match(i, prompt):
                return 'a'
    except:
        for i in create_chatroom_regex_rules:
            if re.match(i, prompt):
                return 'a'
    return None


# 加入语聊室
async def join_chatroom_relu(prompt):
    join_chatroom_relu.matching_type = 'regex'
    try:
        for i in compiled_rules["join_chatroom"]:
            if re.match(i, prompt):
                return 'b'
    except:
        for i in join_chatroom_regex_relus:
            if re.match(i, prompt):
                return 'b'
    return None


# 法律咨询2正则规则
law_regex_rules = [
    re.compile(
        r'.*(你(能|可以)|告诉我|要|我(需要|想要|请求)|发起|开始|进行|帮?我(想|要)(找|咨询一下))?(法律|律师)(咨询|援助|帮助|服务|事务所).*'),
    re.compile(r'.*(告诉我|播报|我需?要|(讲|说|介绍)一(下|个))(律所|律师)(电话|律师事务所).*'),
    re.compile(r'.*(我|你)可以(咨询|回答|问你)法律(相关)?问题吗.*'),
    re.compile(r'.*我要打官司.*')
]


# 法律咨询2
async def law_relu(prompt):
    law_relu.matching_type = "regex"
    try:
        for i in compiled_rules["law"]:
            if re.match(i, prompt):
                return '2'
    except:
        for i in law_regex_rules:
            if re.match(i, prompt):
                return '2'
    return None


# 卡车维修3正则规则
repair_regex_rules = [
    re.compile(r'.*(我的?卡?车(出问题了|坏了)(怎么办？?)?|(你|我)可以(回答|问你)卡车维修的问题吗？?).*'),
    re.compile(r'.*(我((想|需)?要))?维修(卡|汽)车(联系)?(电话)?.*'),
    re.compile(r'.*(卡车)?(维修|售后)(服务|联系)(电话|服务).*'),
    re.compile(r'.*卡车维修.*'),
    re.compile(r'.*保养(卡车|重卡).*'),
    re.compile(r'.*(卡车|重卡)保养.*')
]


# 卡车维修3
async def automobile_maintenance_relu(prompt):
    automobile_maintenance_relu.matching_type = "regex"
    try:
        for i in compiled_rules["repair"]:
            if re.match(i, prompt):
                return '3'
    except:
        for i in repair_regex_rules:
            if re.match(i, prompt):
                return '3'
    return None


# 车祸4正则规则
accident_regex_rules = [
    re.compile(r'.*(出现)?车祸了?(怎么办|找谁)?.*'),
    re.compile(r'.*(撞车|追尾|撞人|(交通|卡车)事故).*'),
    re.compile(r'.*(报警|火警|急救)电话.*')
]


# 车祸4
async def car_accident_relu(prompt):
    car_accident_relu.matching_type = "regex"
    try:
        for i in compiled_rules["accident"]:
            if re.match(i, prompt):
                return '4'
    except:
        for i in accident_regex_rules:
            if re.match(i, prompt):
                return '4'
    return None


# 卡车购买推荐5正则规则
buy_truck_regex_relus = [
    re.compile(r'.*我?(想|要)?(购?买|买一辆)新?(卡车|重卡).*'),
    re.compile(r'.*(我(要|想))?买(新|牵引|载货|自卸|(解放)?卡)车.*'),
    re.compile(r'.*怎么买卡?车.*'),
    re.compile(r'.*(小卡你能给我一些)?卡车(购买)?(电话|推荐|意见|建议)吗?.*'),
    re.compile(r'.*(解放|一汽)(卡车|重卡)购买.*'),
    re.compile(r'.*售车(电话|热线)?.*')
]


# 卡车购买推荐5
async def buy_truck_relu(prompt):
    buy_truck_relu.matching_type = 'regex'
    try:
        for i in compiled_rules["buy_truck"]:
            if re.match(i, prompt):
                return '5'
    except:
        for i in buy_truck_regex_relus:
            if re.match(i, prompt):
                return '5'
    return None


# 卡车物资站7正则规则
truck_material_station_regex_relus = [
    re.compile(r'.*(前往|打开)?(卡车)?(奖品中心|商城|物资站).*'),
    re.compile(r'.*(购买|兑换|卡车)(生活)?(物资|物品).*'),
    re.compile(r'.*卡车购买咨询.*')
]


# 卡车物资站7
async def truck_material_station_relu(prompt):
    truck_material_station_relu.matching_type = 'regex'
    try:
        for i in compiled_rules["truck_material_station"]:
            if re.match(i, prompt):
                return '7'
    except:
        for i in truck_material_station_regex_relus:
            if re.match(i, prompt):
                return '7'
    return None


# 打开社区8正则规则
open_forum_regex_relus = [
    re.compile(r'.*(打开|前往|去)?(卡友|卡车)?(社区).*')
]


# 打开社区8
async def open_forum_relu(prompt):
    open_forum_relu.matching_type = 'regex'
    try:
        for i in compiled_rules["open_forum"]:
            if re.match(i, prompt):
                return '8'
    except:
        for i in open_forum_regex_relus:
            if re.match(i, prompt):
                return '8'
    return None


# 天气9正则规则
weather_regex_rules = [
    re.compile(
        r'((.*(小卡)*[，。！？]?(播报|告诉我|讲一下|说一下)(现在|(.天))?.?天气(咋|怎么)?样?(预报)?(吗|啊)?[，。！？]?(小卡)?)|当前天气).*'),
    re.compile(r'.*(小卡)*[，。！？]?(播报|告诉我|讲一下|说一下)?(现在|(.天))?.?气温(吗|啊)?[，。！？]?(小卡)?.*'),
    re.compile(r'^(小卡)*[，。！？]?(播报|告诉我|讲一下|说一下)?(现在|(.天))?.?温度(吗|啊)?[，。！？]?(小卡)?$'),
    re.compile(r'.*(今天|现在)?((天气|温度)((怎么|咋)样|好吗)|适合外出吗|(几|多少)度).*'),
    re.compile(r'^(小卡)*[，。！？]?(天气|温度)[？，。！]?$')
]


# 天气9
async def weather_relu(prompt):
    weather_relu.matching_type = 'regex'
    try:
        for i in compiled_rules["weather"]:
            if re.match(i, prompt):
                return '9'
    except:
        for i in weather_regex_rules:
            if re.match(i, prompt):
                return '9'
    return None


# 打招呼10正则规则
greeting_regex_rules = [
    re.compile(r'.*(小卡)*[，。！？]?(您好|早上好|hello|hi|hey|晚上好|上午好|中午好|下午好)[，。！？]?啊?(小卡)?',
               re.IGNORECASE),
    re.compile(r'^(小卡)*[，。！？]?(你好)[，。！？]?啊?(小卡)?[？，。！]?$', re.IGNORECASE),
]


# 打招呼10
async def greeting_relu(prompt):
    greeting_relu.matching_type = 'regex'
    try:
        for i in compiled_rules["greeting"]:
            if re.match(i, prompt):
                return '10'
    except:
        for i in greeting_regex_rules:
            if re.match(i, prompt):
                return '10'
    return None


# 时间11正则规则
time_regex_rules = [
    re.compile(
        r'^(小卡)*[，。！？]?(((播报|告诉我|讲一下|说一下)?(当前|现在)?.?(什么)?时间(吗|啊|了)?[，。！？]?(小卡)?$)|当前时间|现在是?什么(时候|时间)(了|呢)啊?|现在是?什么(时候|时间)(了|呢)?啊?)[，。！？]?$'),
    re.compile(r'.*(小卡)*[，。！？]?(告诉我|播报|讲一下|说一下)?.*几点钟?了?(吗|啊)?[，。！？]?(小卡)?$'),
    re.compile(r'.*(小卡)*[，。！？]?(告诉我|播报|讲一下|说一下)?.*时辰了?(吗|啊)?[，。！？]?(小卡)?$')
]


# 时间11
async def time_relu(prompt):
    time_relu.matching_type = 'regex'
    try:
        for i in compiled_rules["time"]:
            if re.match(i, prompt):
                return '11'
    except:
        for i in time_regex_rules:
            if re.match(i, prompt):
                return '11'
    return None


# 日期、星期几正则规则
date_regex_relus = [
    re.compile(r'^(小卡)*.?[，。！？]*(今天|现在|今儿?个)?(几月)?(几号了?|(周|星期|礼拜)几)(啊|呢|呀)?[？，。！]?$')
]


async def date_relu(prompt):
    date_relu.matching_type = 'regex'
    try:
        for i in compiled_rules["date"]:
            if re.match(i, prompt):
                return 'date'
    except:
        for i in date_regex_relus:
            if re.match(i, prompt):
                return 'date'
    return None


# 人工服务12正则规则
service_regex_relus = [
    re.compile(r'.*(告诉我|播报|(讲|说)(一下)?|联系)?(人工|客服)(电话|服务|客服)(是多少)?.*')
]


# 人工服务12
async def service_relu(prompt):
    service_relu.matching_type = 'regex'
    try:
        for i in compiled_rules["service"]:
            if re.match(i, prompt):
                return '12'
    except:
        for i in service_regex_relus:
            if re.match(i, prompt):
                return '12'
    return None


# 个人中心26正则规则
personal_center_regex_relus = [
    re.compile(r'.*个人中心.*')
]


# 个人中心26
async def personal_center_relu(prompt):
    try:
        for i in compiled_rules["personal_center"]:
            if re.match(i, prompt):
                return '26'
    except:
        for i in personal_center_regex_relus:
            if re.match(i, prompt):
                return '26'
    return None


# 活动资讯14正则规则
activity_info_regex_relus = [
    re.compile(r'.*(播报|告诉我|讲一下|说一下)?活动(资|咨)(讯|询).*'),
    re.compile(r'.*(最近有什么(新的)?卡车(4s店)活动|4s店活动).*', re.IGNORECASE)
]


# 活动资讯14
async def activity_info_relu(prompt):
    activity_info_relu.matching_type = 'regex'
    try:
        for i in compiled_rules["activity_info"]:
            if re.match(i, prompt):
                return '14'
    except:
        for i in activity_info_regex_relus:
            if re.match(i, prompt):
                return '14'
    return None


# 外卖15正则规则
takeaway_regex_relus = [
    re.compile(r'.*(打开外卖软件|我((想|要)(点外卖|吃东西)|(肚子)?饿了)).*'),
    re.compile(r'.*吃(宵夜|(早|晚|午)(餐|饭)).*'),
    re.compile(r'点?外卖[？，。！]?')
]


# 外卖15
async def takeaway_relu(prompt):
    takeaway_relu.matching_type = 'regex'
    try:
        for i in compiled_rules["takeaway"]:
            if re.match(i, prompt):
                return '15'
    except:
        for i in takeaway_regex_relus:
            if re.match(i, prompt):
                return '15'
    return None


# 导航16正则规则
navigation_regex_relus = [
    re.compile(
        r'.*(告诉我|播报|(讲|说)一下|(最近|附近)的?|(我(想|需?要))?(去|前|开)往)加油站(在哪|离我(还?有)多远)?.*'),
    re.compile(
        r'.*(告诉我|播报|(讲|说)一下|(最近|附近)的?|(我(想|需?要))?(去|前|开)往)?加油站(在哪|离我(还?有)多远).*'),
    re.compile(r'.*(告诉我|播报|当前|实时)(当前|实时)?路况.*'),
    re.compile(r'.*(打开)?导航(地图)?(软件)?.*'),
    re.compile(r'.*打开地图(软件)?.*'),
    re.compile(r'^(小卡)*.?[，。！？]*加油站[？，。！]$')
]


# 导航16
async def map_relu(prompt):
    map_relu.matching_type = 'regex'
    try:
        for i in compiled_rules["navigation"]:
            if re.match(i, prompt):
                return '16'
    except:
        for i in navigation_regex_relus:
            if re.match(i, prompt):
                return '16'
    return None


# 新闻资讯17正则规则
news_regex_relus = [
    re.compile(r'.*(播报|我想要知道)(新闻)?咨(讯|询).*'),
    re.compile(r'.*(告诉我|(说|讲)一下)资讯.*'),
    re.compile(r'.*((打开)?(新闻|阅读|新闻阅读)软件|我(想|要)看新闻).*')
]


# 新闻阅读17
async def news_relu(prompt):
    news_relu.matching_type = 'regex'
    try:
        for i in compiled_rules["news"]:
            if re.match(i, prompt):
                return '17'
    except:
        for i in news_regex_relus:
            if re.match(i, prompt):
                return '17'
    return None


# 社交通讯18正则规则
social_communication_regex_relus = [
    re.compile(r'.*(打开(社交|聊天|通讯)软件|我想(和朋友)?聊天).*')
]


# 社交通讯18
async def social_communication_relu(prompt):
    social_communication_relu.matching_type = 'regex'
    try:
        for i in compiled_rules["social_communication"]:
            if re.match(i, prompt):
                return '18'
    except:
        for i in social_communication_regex_relus:
            if re.match(i, prompt):
                return '18'
    return None


# 生活分享19正则规则
life_sharing_regex_relus = [
    re.compile(r'.*打开(短视频|生活分享)软件.*'),
    re.compile(r'.*(我(想|要))?(看|发)短视频.*'),
]


# 生活分享19
async def life_sharing_relu(prompt):
    life_sharing_relu.matching_type = 'regex'
    try:
        for i in compiled_rules["life_sharing"]:
            if re.match(i, prompt):
                return '19'
    except:
        for i in life_sharing_regex_relus:
            if re.match(i, prompt):
                return '19'
    return None


# 浏览器20正则规则
browser_regex_relus = [
    re.compile(r'.*打开(浏览器|搜索)(软件)?.*')
]


# 浏览器20
async def browser_relu(prompt):
    life_sharing_relu.matching_type = 'regex'
    try:
        for i in compiled_rules["browser"]:
            if re.match(i, prompt):
                return '20'
    except:
        for i in browser_regex_relus:
            if re.match(i, prompt):
                return '20'
    return None


# 影音娱乐21正则规则
movie_entertainment_regex_relus = [
    re.compile(r'.*打开(视频|看电影的)软件.*'),
    re.compile(r'.*(我(想|要))?看(电(影|视剧)|动漫|综艺).*'),
]


# 影音娱乐21
async def movie_entertainment_relu(prompt):
    movie_entertainment_relu.matching_type = 'regex'
    try:
        for i in compiled_rules["movie_entertainment"]:
            if re.match(i, prompt):
                return '21'
    except:
        for i in movie_entertainment_regex_relus:
            if re.match(i, prompt):
                return '21'
    return None


# 音乐视听22正则规则
music_listening_regex_relus = [
    re.compile(r'.*(打开音乐软件|我(想|要)听(歌|音乐|小说)).*')
]


# 音乐视听22
async def music_listening_relu(prompt):
    music_listening_relu.matching_type = 'regex'
    try:
        for i in compiled_rules["music_listening"]:
            if re.match(i, prompt):
                return '22'
    except:
        for i in music_listening_regex_relus:
            if re.match(i, prompt):
                return '22'
    return None


# 违章处理23正则规则
traffic_violation_regex_relus = [
    re.compile(r'.*((打开)?违章处理(软件)?|(我要处理)?违章记录|处理违章).*')
]


# 违章处理23
async def traffic_violation_relu(prompt):
    traffic_violation_relu.matching_type = 'regex'
    try:
        for i in compiled_rules["traffic_violation"]:
            if re.match(i, prompt):
                return '23'
    except:
        for i in traffic_violation_regex_relus:
            if re.match(i, prompt):
                return '23'
    return None


# 支付24正则规则
payment_regex_relus = [
    re.compile(r'.*(打开(支付|付款)软件|我要(付(款|钱)|(结|转)账)).*')
]


# 支付24
async def payment_relu(prompt):
    payment_relu.matching_type = 'regex'
    try:
        for i in compiled_rules["payment"]:
            if re.match(i, prompt):
                return '24'
    except:
        for i in payment_regex_relus:
            if re.match(i, prompt):
                return '24'
    return None


# 网络购物25正则规则
online_shopping_regex_relus = [
    re.compile(r'.*(打开(网络)?购物软件|(我(要|想))?买东西|网络购物|网上买商品).*')
]


# 网络购物25
async def online_shopping_relu(prompt):
    online_shopping_relu.matching_type = 'regex'
    try:
        for i in compiled_rules["online_shopping"]:
            if re.match(i, prompt):
                return '25'
    except:
        for i in online_shopping_regex_relus:
            if re.match(i, prompt):
                return '25'
    return None


# 跳舞54正则规则
dance_regex_relus = [
    re.compile(r'.*(展示)?(舞蹈|(跳|来)一?(个|支|段)舞).*'),
    re.compile(r'.*你(能|会)跳舞吗[？，。！]?'),
    re.compile(r'^(小+卡)*[？，。！]?跳舞[？，。！]?')
]


# 跳舞54
async def dance_relu(prompt):
    dance_relu.matching_type = 'regex'
    try:
        for i in compiled_rules["dance"]:
            if re.match(i, prompt):
                return '54'
    except:
        for i in dance_regex_relus:
            if re.match(i, prompt):
                return '54'
    return None


# 货源55正则规则
source_info_regex_relus = [
    re.compile(r'.*打开货源(信息)?软件.?.*'),
    re.compile(r'.*((帮我|我想)找一下)?(卡车)?货源.*')
]


# 货源55
async def source_info_relu(prompt):
    source_info_relu.matching_type = 'regex'
    try:
        for i in compiled_rules["source_info"]:
            if re.match(i, prompt):
                return '55'
    except:
        for i in source_info_regex_relus:
            if re.match(i, prompt):
                return '55'
    return None


# 卡车论坛56正则规则
truck_community_regex_relus = [
    re.compile(r'.*(打开|前往)(卡车)?论坛(软件)?.*'),
    re.compile(r'.*(卡|车)(友|车)论坛.*')
]


# 卡车论坛56
async def truck_community_relu(prompt):
    truck_community_relu.matching_type = 'regex'
    try:
        for i in compiled_rules["truck_community"]:
            if re.match(i, prompt):
                return '56'
    except:
        for i in truck_community_regex_relus:
            if re.match(i, prompt):
                return '56'
    return None


# 打开APP正则规则
app_regex_relus = {
    '27': re.compile(r'.*(?<!别)打开美团外卖[。，？！]?'),
    '28': re.compile(r'.*(?<!别)打开饿了么[。，？！]?'),
    '29': re.compile(r'.*(?<!别)打开高德(地图)?[。，？！]?'),
    '30': re.compile(r'.*(?<!别)打开百度地图[。，？！]?'),
    '31': re.compile(r'.*(?<!别)打开腾讯地图[。，？！]?'),
    '32': re.compile(r'.*(?<!别)打开今日头条[。，？！]?'),
    '33': re.compile(r'.*(?<!别)打开腾讯新闻[。，？！]?'),
    '34': re.compile(r'.*(?<!别)打开网易新闻[。，？！]?'),
    '35': re.compile(r'.*(?<!别)打开QQ[。，？！]?', re.IGNORECASE),
    '36': re.compile(r'.*(?<!别)打开微信[。，？！]?'),
    '37': re.compile(r'.*(?<!别)打开抖音[。，？！]?'),
    '38': re.compile(r'.*(?<!别)打开快手[。，？！]?'),
    '39': re.compile(r'.*(?<!别)打开腾讯视频[。，？！]?'),
    '40': re.compile(r'.*(?<!别)打开爱奇艺[。，？！]?'),
    '41': re.compile(r'.*(?<!别)打开优酷(视频)?[。，？！]?'),
    '42': re.compile(r'.*(?<!别)打开UC浏览器[。，？！]?', re.IGNORECASE),
    '43': re.compile(r'.*(?<!别)打开百度浏览器[。，？！]?'),
    '44': re.compile(r'.*(?<!别)打开QQ浏览器[。，？！]?', re.IGNORECASE),
    '45': re.compile(r'.*(?<!别)打开QQ音乐[。，？！]?', re.IGNORECASE),
    '46': re.compile(r'.*(?<!别)打开酷狗(音乐)?[。，？！]?'),
    '47': re.compile(r'.*(?<!别)打开网易云(音乐)?[。，？！]?'),
    '48': re.compile(r'.*(?<!别)打开喜马拉雅(音乐)?[。，？！]?'),
    '49': re.compile(r'.*(?<!别)打开交管(12123)?[。，？！]?'),
    '50': re.compile(r'.*(?<!别)打开支付宝[。，？！]?'),
    '51': re.compile(r'.*(?<!别)打开淘宝[。，？！]?'),
    '52': re.compile(r'.*(?<!别)打开京东[。，？！]?'),
    '53': re.compile(r'.*(?<!别)打开拼多多[。，？！]?'),
    '57': re.compile(r'.*(?<!别)打开运满满[。，？！]?'),
    '58': re.compile(r'.*(?<!别)打开货车帮[。，？！]?'),
    '59': re.compile(r'.*(?<!别)打开货拉拉[。，？！]?'),
    '60': re.compile(r'.*(?<!别)打开(五八|58)同城[。，？！]?'),
    '61': re.compile(r'.*(?<!别)打开卡车之家[。，？！]?'),
    '72': re.compile(r'.*(?<!别)打开美团[。，？！]?')
}


# 打开APP
async def open_app(prompt):
    if re.match(app_regex_relus['27'], prompt):
        return '27'
    elif re.match(app_regex_relus['28'], prompt):
        return '28'
    elif re.match(app_regex_relus['29'], prompt):
        return '29'
    elif re.match(app_regex_relus['30'], prompt):
        return '30'
    elif re.match(app_regex_relus['31'], prompt):
        return '31'
    elif re.match(app_regex_relus['32'], prompt):
        return '32'
    elif re.match(app_regex_relus['33'], prompt):
        return '33'
    elif re.match(app_regex_relus['34'], prompt):
        return '34'
    elif re.match(app_regex_relus['44'], prompt):
        return '44'
    elif re.match(app_regex_relus['36'], prompt):
        return '36'
    elif re.match(app_regex_relus['37'], prompt):
        return '37'
    elif re.match(app_regex_relus['38'], prompt):
        return '38'
    elif re.match(app_regex_relus['39'], prompt):
        return '39'
    elif re.match(app_regex_relus['40'], prompt):
        return '40'
    elif re.match(app_regex_relus['41'], prompt):
        return '41'
    elif re.match(app_regex_relus['42'], prompt):
        return '42'
    elif re.match(app_regex_relus['43'], prompt):
        return '43'
    elif re.match(app_regex_relus['45'], prompt):
        return '45'
    elif re.match(app_regex_relus['35'], prompt):
        return '35'
    elif re.match(app_regex_relus['46'], prompt):
        return '46'
    elif re.match(app_regex_relus['47'], prompt):
        return '47'
    elif re.match(app_regex_relus['48'], prompt):
        return '48'
    elif re.match(app_regex_relus['49'], prompt):
        return '49'
    elif re.match(app_regex_relus['50'], prompt):
        return '50'
    elif re.match(app_regex_relus['51'], prompt):
        return '51'
    elif re.match(app_regex_relus['52'], prompt):
        return '52'
    elif re.match(app_regex_relus['53'], prompt):
        return '53'
    elif re.match(app_regex_relus['57'], prompt):
        return '57'
    elif re.match(app_regex_relus['58'], prompt):
        return '58'
    elif re.match(app_regex_relus['59'], prompt):
        return '59'
    elif re.match(app_regex_relus['60'], prompt):
        return '60'
    elif re.match(app_regex_relus['61'], prompt):
        return '61'
    elif re.match(app_regex_relus['72'], prompt):
        return '72'
    else:
        return None


# 切换情感模式正则规则
emotion_mode_regex_relus = [
    re.compile(r'.*(打开|切换|前往)情感模式.*')
]


# 切换情感模式62
async def emotion_mode_relu(prompt):
    emotion_mode_relu.matching_type = 'regex'

    try:
        for i in compiled_rules["emotion_mode"]:
            if re.match(i, prompt):
                return '62'
    except:
        for i in emotion_mode_regex_relus:
            if re.match(i, prompt):
                return '62'
    return None


# 附近/位置问题正则规则
neighborhood_problem_regex_relus = [
    re.compile(r'.*(附近|身边|旁边|周围|边上)(哪里)?有.*'),
    re.compile(r'^(小卡)*[，。！？]?(当前)?位置[？，。！]?$'),
    re.compile(r'.*(小卡)*[，。！？]?我(现在|当前)(位置)?在哪[？，。！]?.*')
]


# 附近问题
async def neighborhood_problem_relu(prompt):
    neighborhood_problem_relu.matching_type = 'regex'
    try:
        for i in compiled_rules["neighborhood_problem"]:
            if re.match(i, prompt):
                return '附近'
    except:
        for i in neighborhood_problem_regex_relus:
            if re.match(i, prompt):
                return '附近'
    return None


# 任务中心正则规则
task_center_regex_relus = [
    re.compile(r'.*(打开|去|前往|跳转)?任务中心.*')
]


# 任务中心
async def task_center_relu(prompt):
    task_center_relu.matching_type = 'regex'
    try:
        for i in compiled_rules["task_center"]:
            if re.match(i, prompt):
                return '63'
    except:
        for i in task_center_regex_relus:
            if re.match(i, prompt):
                return '63'
    return None


# 邀请推广正则规则
invite_promotion_regex_relus = [
    re.compile(r'.*(打开|去|前往|跳转)?(邀请|推广|邀请推广|推广邀请).*'),
    re.compile(r'.*(打开|前往)(邀请|推广).*')
]


# 邀请推广
async def invite_promotion_relu(prompt):
    invite_promotion_relu.matching_type = 'regex'
    try:
        for i in compiled_rules["invite_promotion"]:
            if re.match(i, prompt):
                return '64'
    except:
        for i in invite_promotion_regex_relus:
            if re.match(i, prompt):
                return '64'
    return None


# 帮助中心正则规则
help_center_regex_relus = [
    re.compile(r'.*(打开|去|前往|跳转)?帮助中心.*'),
    re.compile(r'.*(打开|前往)帮助.*')
]


# 帮助中心
async def help_center_relu(prompt):
    help_center_relu.matching_type = 'regex'
    try:
        for i in compiled_rules["help_center"]:
            if re.match(i, prompt):
                return '65'
    except:
        for i in help_center_regex_relus:
            if re.match(i, prompt):
                return '65'
    return None


# 更换背景正则规则
change_background_regex_relus = [
    re.compile(r'.*(打开|场景|更换)装扮.*'),
    re.compile(r'.*(前往|小卡)换装.*'),
    re.compile(r'.*(服装|装扮|场景|背景)更换.*'),
    re.compile(r'.*更换一?个?(服装|装扮|场景|背景).*')
]


# 更换背景66
async def change_background_relu(prompt):
    change_background_relu.matching_type = 'regex'
    try:
        for i in compiled_rules["change_background"]:
            if re.match(i, prompt):
                return '66'
    except:
        for i in change_background_regex_relus:
            if re.match(i, prompt):
                return '66'
    return None


# 东风客服电话67
DONGFENG_service_regex_relus = [
    re.compile(r'.*东风((卡|商用)车)?(服务|客服|售后)(电话|热线).*')
]


# 东风客服67
async def dongfeng_service_relu(prompt):
    dongfeng_service_relu.matching_type = 'regex'
    try:
        for i in compiled_rules["dongfeng_service"]:
            if re.match(i, prompt):
                return '67'
    except:
        for i in DONGFENG_service_regex_relus:
            if re.match(i, prompt):
                return '67'
    return None


# 陕汽客服电话68
SHANQI_service_regex_relus = [
    re.compile(r'.*陕汽((卡|商用)车)?(服务|客服|售后)(电话|热线).*')
]


# 陕汽客服68
async def shanqi_service_relu(prompt):
    shanqi_service_relu.matching_type = 'regex'
    try:
        for i in compiled_rules["shanqi_service"]:
            if re.match(i, prompt):
                return '68'
    except:
        for i in SHANQI_service_regex_relus:
            if re.match(i, prompt):
                return '68'
    return None


# 中国重汽客服电话69
SINOTRUK_service_regex_relus = [
    re.compile(r'.*(中国)?重汽(卡车)?(客服|服务|售后)(电话|热线).*')
]


# 中国重汽客服69
async def sinotruk_service_relu(prompt):
    sinotruk_service_relu.matching_type = 'regex'
    try:
        for i in compiled_rules["sinotruk_service"]:
            if re.match(i, prompt):
                return '69'
    except:
        for i in SINOTRUK_service_regex_relus:
            if re.match(i, prompt):
                return '69'
    return None


# 福田客服电话70
FOTON_service_regex_relus = [
    re.compile(r'.*(福田|欧曼)(卡车|戴勒姆)?(服务|客服|售后)(电话|热线).*')
]


# 福田客服70
async def foton_service_relu(prompt):
    foton_service_relu.matching_type = 'regex'
    try:
        for i in compiled_rules["foton_service"]:
            if re.match(i, prompt):
                return '70'
    except:
        for i in FOTON_service_regex_relus:
            if re.match(i, prompt):
                return '70'
    return None


# 解放客服电话71
FAW_service_regex_relus = [
    re.compile(r'.*(一汽)?解放(卡车)?(服务|客服|售后)(电话|热线).*')
]


# 福田客服71
async def faw_service_relu(prompt):
    faw_service_relu.matching_type = 'regex'
    try:
        for i in compiled_rules["faw_service"]:
            if re.match(i, prompt):
                return '71'
    except:
        for i in FAW_service_regex_relus:
            if re.match(i, prompt):
                return '71'
    return None


# 意图识别
async def intent_recognition(prompt):
    result = await buy_truck_relu(prompt)
    if result is not None:
        return result

    result = await emotion_mode_relu(prompt)
    if result is not None:
        return result

    result = await open_app(prompt)
    if result is not None:
        return result

    result = await activity_info_relu(prompt)
    if result is not None:
        return result

    result = await dance_relu(prompt)
    if result is not None:
        return result

    result = await automobile_maintenance_relu(prompt)
    if result is not None:
        return result

    result = await truck_material_station_relu(prompt)
    if result is not None:
        return result

    result = await open_forum_relu(prompt)
    if result is not None:
        return result

    result = await faw_service_relu(prompt)
    if result is not None:
        return result

    result = await dongfeng_service_relu(prompt)
    if result is not None:
        return result

    result = await shanqi_service_relu(prompt)
    if result is not None:
        return result

    result = await sinotruk_service_relu(prompt)
    if result is not None:
        return result

    result = await foton_service_relu(prompt)
    if result is not None:
        return result

    result = await service_relu(prompt)
    if result is not None:
        return result

    result = await personal_center_relu(prompt)
    if result is not None:
        return result

    result = await takeaway_relu(prompt)
    if result is not None:
        return result

    result = await map_relu(prompt)
    if result is not None:
        return result

    result = await news_relu(prompt)
    if result is not None:
        return result

    result = await social_communication_relu(prompt)
    if result is not None:
        return result

    result = await life_sharing_relu(prompt)
    if result is not None:
        return result

    result = await source_info_relu(prompt)
    if result is not None:
        return result

    result = await truck_community_relu(prompt)
    if result is not None:
        return result

    result = await browser_relu(prompt)
    if result is not None:
        return result

    result = await movie_entertainment_relu(prompt)
    if result is not None:
        return result

    result = await music_listening_relu(prompt)
    if result is not None:
        return result

    result = await traffic_violation_relu(prompt)
    if result is not None:
        return result

    result = await payment_relu(prompt)
    if result is not None:
        return result

    result = await online_shopping_relu(prompt)
    if result is not None:
        return result

    result = await create_chatroom_relu(prompt)
    if result is not None:
        return result

    result = await join_chatroom_relu(prompt)
    if result is not None:
        return result

    result = await task_center_relu(prompt)
    if result is not None:
        return result

    result = await invite_promotion_relu(prompt)
    if result is not None:
        return result

    result = await help_center_relu(prompt)
    if result is not None:
        return result

    result = await change_background_relu(prompt)
    if result is not None:
        return result

    # result = await greeting_relu(prompt)
    # if result is not None:
    #     return result

    # 如果没有匹配到任何意图，则返回None
    return None
